package lc;
import java.util.*;

import org.junit.Test;

import util.ListNode;

public class Ex24 {
    class Solution {
        public ListNode swapPairs(ListNode head) {
            if (head == null) return head;
            ListNode tmp, pre = head, post = head.next, res = new ListNode(0), result = res;
            res.next = head;
    
            while (post != null && post.next != null) {
                tmp = post.next;
                post.next = pre;
                pre.next = tmp;
                res.next = post;
                res = pre;
                pre = tmp;
                post = tmp.next;
            }
    
            return result.next;
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        int[] nums = new int[]{1,2,3,4};
        ListNode head = ListNode.newListNodeFromArray(nums);
        head = s.swapPairs(head);
        List<Integer> res = new ArrayList<>();
        Collections.reverse(res);
    }
}
